package org.come.Jpanel;

import com.tool.btn.HuangBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import org.come.Frame.ZhuFrame;

import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class HuangJpanel extends JPanel implements MouseListener {//黄大小
    static ImageIcon icon = new ImageIcon("inkImg/background/黄大小水墨.png");
    static ImageIcon icon1 = new ImageIcon("inkImg/hongmu/黄大小红木.png");
    private Timer countdownTimer;
    private Timer npcAnnouncementTimer; // 新增的定时器用于NPC喊话
    private int countdownSeconds; // 不再初始化为固定值
    private static HuangBtn xuanyu, jinbi, dashuang, da, xiao, baozi, mai, quxiao;
    private static JTextField gold;
    private int type = 0;
    private int tpye1 = 0;
    public long gold0;
    public static String tou;
    public static String tou1;
    private Random random = new Random();

    public HuangJpanel() {
        if ("水墨UI".equals(MyIsif.getStyle())) {
            this.setPreferredSize(new Dimension(350, 320));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            xuanyu = new HuangBtn("inkImg/button/B59.png", 1, 1, "头彩", this);
            xuanyu.setBounds(40, 80, 68, 40);
            xuanyu.setColors(UIUtils.COLOR_BLACK);
            this.add(xuanyu);
            dashuang = new HuangBtn("inkImg/button/B59.png", 1, 3, "七星", this);
            dashuang.setBounds(xuanyu.getX() + 75, 80, 68, 40);
            dashuang.setColors(UIUtils.COLOR_BLACK);
            this.add(dashuang);
            jinbi = new HuangBtn("inkImg/button/B59.png", 1, 2, "双对", this);
            jinbi.setBounds(dashuang.getX() + 75, 80, 68, 40);
            jinbi.setColors(UIUtils.COLOR_BLACK);
            this.add(jinbi);
            da = new HuangBtn("inkImg/button/B59.png", 1, 4, "散星", this);
            da.setBounds(jinbi.getX() + 75, 80, 68, 40);
            da.setColors(UIUtils.COLOR_BLACK);
            this.add(da);
            xiao = new HuangBtn("inkImg/button/B59.png", 1, 5, "金币", this);
            xiao.setBounds(dashuang.getX() + 75, dashuang.getY() + 40, 68, 40);
            xiao.setColors(UIUtils.COLOR_BLACK);
            this.add(xiao);
            baozi = new HuangBtn("inkImg/button/B59.png", 1, 6, "仙玉", this);
            baozi.setBounds(xiao.getX() + 75, dashuang.getY() + 40, 68, 40);
            baozi.setColors(UIUtils.COLOR_BLACK);
            this.add(baozi);
            mai = new HuangBtn("inkImg/button/18.png", 1, 7, "买定离手", this);
            mai.setBounds(50, 240, 100, 40);
            mai.setColors(UIUtils.COLOR_BLACK);
            this.add(mai);
            quxiao = new HuangBtn("inkImg/button/18.png", 1, 8, "取消选择", this);
            quxiao.setBounds(190, 240, 100, 40);

            quxiao.setColors(UIUtils.COLOR_BLACK);
            this.add(quxiao);
            gold = new JTextField();
            gold.setBounds(185 - 15, 202 + 2, 160, 15);
            gold.setForeground(Color.white);
            gold.setOpaque(false);
            gold.setFont(new Font("宋体", 1, 13));
            gold.setBorder(BorderFactory.createEmptyBorder());
            gold.setCaretColor(Color.white);
            this.add(gold);
            // 初始化倒计时
            countdownSeconds = calculateNextMultipleOfFiveMinutes();
            // 启动倒计时
            countdownTimer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (countdownSeconds > 0) {
                        countdownSeconds--;
                        repaint(); // 重新绘制面板以显示新的倒计时
                    } else {
                        countdownSeconds = calculateNextMultipleOfFiveMinutes(); // 重置倒计时
                        // 可以在这里添加开奖逻辑
                    }
                }
            });
            countdownTimer.start();

            // 启动NPC喊话定时器
            startNpcAnnouncementTimer();
        } else {
            this.setPreferredSize(new Dimension(350, 320));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            xuanyu = new HuangBtn("inkImg/hongmu/6026.png", 1, 1, "头彩", this);
            xuanyu.setBounds(30, 85, 60, 30);
            xuanyu.setColors(UIUtils.COLOR_YELLOW);
            this.add(xuanyu);
            dashuang = new HuangBtn("inkImg/hongmu/6026.png", 1, 3, "七星", this);
            dashuang.setBounds(xuanyu.getX() + 75, 85, 60, 30);
            dashuang.setColors(UIUtils.COLOR_YELLOW);
            this.add(dashuang);
            jinbi = new HuangBtn("inkImg/hongmu/6026.png", 1, 2, "双对", this);
            jinbi.setBounds(dashuang.getX() + 75, 85, 60, 30);
            jinbi.setColors(UIUtils.COLOR_YELLOW);
            this.add(jinbi);
            da = new HuangBtn("inkImg/hongmu/6026.png", 1, 4, "散星", this);
            da.setBounds(jinbi.getX() + 75, 85, 60, 30);
            da.setColors(UIUtils.COLOR_YELLOW);
            this.add(da);
            xiao = new HuangBtn("inkImg/hongmu/6026.png", 1, 5, "金币", this);
            xiao.setBounds(dashuang.getX() + 75, dashuang.getY() + 35, 60, 30);
            xiao.setColors(UIUtils.COLOR_YELLOW);
            this.add(xiao);
            baozi = new HuangBtn("inkImg/hongmu/6026.png", 1, 6, "仙玉", this);
            baozi.setBounds(xiao.getX() + 75, dashuang.getY() + 35, 60, 30);
            baozi.setColors(UIUtils.COLOR_YELLOW);
            this.add(baozi);
            mai = new HuangBtn("inkImg/hongmu/1_png.button.btn_xy.png", 1, 7, "买定离手", this);
            mai.setBounds(60, 200, 80, 30);
            mai.setColors(UIUtils.COLOR_YELLOW);
            this.add(mai);
            quxiao = new HuangBtn("inkImg/hongmu/1_png.button.btn_xy.png", 1, 8, "取消选择", this);
            quxiao.setBounds(210, 200, 80, 30);

            quxiao.setColors(UIUtils.COLOR_YELLOW);
            this.add(quxiao);
            gold = new JTextField();
            gold.setBounds(138, 165, 160, 15);
            gold.setForeground(Color.white);
            gold.setOpaque(false);
            gold.setFont(new Font("宋体", 1, 13));
            gold.setBorder(BorderFactory.createEmptyBorder());
            gold.setCaretColor(Color.white);
            this.add(gold);
            // 初始化倒计时
            countdownSeconds = calculateNextMultipleOfFiveMinutes();
            // 启动倒计时
            countdownTimer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (countdownSeconds > 0) {
                        countdownSeconds--;
                        repaint(); // 重新绘制面板以显示新的倒计时
                    } else {
                        countdownSeconds = calculateNextMultipleOfFiveMinutes(); // 重置倒计时
                        // 可以在这里添加开奖逻辑
                    }
                }
            });
            countdownTimer.start();

            // 启动NPC喊话定时器
            startNpcAnnouncementTimer();
        }
    }

    private int calculateNextMultipleOfFiveMinutes() {
        long currentTime = System.currentTimeMillis() / 1000;
        int currentMinute = (int) (currentTime / 60);
        int remainder = currentMinute % 5;
        int nextMultipleOfFive = (remainder == 0) ? currentMinute : currentMinute + (5 - remainder);
        return (nextMultipleOfFive * 60) - currentMinute * 60;
    }

    private void startNpcAnnouncementTimer() {
        npcAnnouncementTimer = new Timer(getRandomInterval(), new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                // 重新设置定时器的时间间隔
                npcAnnouncementTimer.setDelay(getRandomInterval());
            }
        });
        npcAnnouncementTimer.start();
    }

    private int getRandomInterval() {
        // 随机生成一个10到60秒之间的间隔
        return (random.nextInt(200) + 10) * 1000; // 10到60秒之间
    }



    public static String getTou() {
        return tou;
    }

    public static void setTou(String tou) {
        HuangJpanel.tou = tou;
    }

    public static String getTou1() {
        return tou1;
    }

    public static void setTou1(String tou1) {
        HuangJpanel.tou1 = tou1;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if ("水墨UI".equals(MyIsif.getStyle())) {
            if (icon != null)
                g.drawImage(icon.getImage(), 0, 0, 360, 318, this);
            if (gold.getText().length() != 0 && gold.getText().matches("\\d+")) {
                gold0 = Long.parseLong(gold.getText());
                if (getTpye1() == 6) {
                    if (Long.parseLong(gold.getText()) > 100000) {
                        ZhuFrame.getZhuJpanel().addPrompt("#R最多押注10W！");
                        gold.setText("100000");
                    }
                } else {
                    if (Long.parseLong(gold.getText()) > 10000000000L) {
                        ZhuFrame.getZhuJpanel().addPrompt("#R最多押注100E！");
                        gold.setText("10000000000");
                    }
                }
            } else gold0 = 0;
            Font font = new Font("宋体", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.blue);
            if (tou != null && tou1 != null && !tou.equals("null") && !tou1.equals("null")) {
                g.setColor(Color.red);
                g.drawString(String.valueOf(tou), 150, 69);
                g.drawString("，", 165, 69);
                g.drawString(String.valueOf(tou1), 180, 69);
            } else {
                g.setColor(Color.red);
                g.drawString("请等待头彩号码开启", 150, 69);
            }
            // 绘制倒计时
            g.setColor(Color.black); // 设置颜色为黑色
            g.setFont(new Font("宋体", Font.BOLD, 13)); // 设置字体大小为18
            g.drawString("本局结束: " + countdownSeconds + " 秒", 210, 300);
        } else {
            if (icon1 != null)
                g.drawImage(icon1.getImage(), 0, 0, 350, 320, this);
            if (gold.getText().length() != 0 && gold.getText().matches("\\d+")) {
                gold0 = Long.parseLong(gold.getText());
                if (getTpye1() == 6) {
                    if (Long.parseLong(gold.getText()) > 100000) {
                        ZhuFrame.getZhuJpanel().addPrompt("#R最多押注10W！");
                        gold.setText("100000");
                    }
                } else {
                    if (Long.parseLong(gold.getText()) > 10000000000L) {
                        ZhuFrame.getZhuJpanel().addPrompt("#R最多押注100E！");
                        gold.setText("10000000000");
                    }
                }
            } else gold0 = 0;
            Font font = new Font("宋体", Font.BOLD, 20);
            g.setFont(font);
            g.setColor(Color.orange);
            if (tou != null && tou1 != null && !tou.equals("null") && !tou1.equals("null")) {
                g.setColor(Color.red);
                g.drawString(String.valueOf(tou), 120, 74);
                g.drawString("，", 135, 74);
                g.drawString(String.valueOf(tou1), 150, 74);
            } else {
                g.setColor(Color.red);
                g.drawString("请等待头彩号码开启", 120, 72);
            }
            // 绘制倒计时
            g.setColor(Color.yellow); // 设置颜色为黑色
            g.setFont(new Font("宋体", Font.BOLD, 13)); // 设置字体大小为18
            g.drawString("本局结束: " + countdownSeconds + " 秒", 210, 300);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public static ImageIcon getIcon() {
        return icon;
    }

    public static void setIcon(ImageIcon icon) {
        HuangJpanel.icon = icon;
    }

    public static HuangBtn getDashuang() {
        return dashuang;
    }

    public void setDashuang(HuangBtn dashuang) {
        this.dashuang = dashuang;
    }

    public static HuangBtn getDa() {
        return da;
    }

    public void setDa(HuangBtn da) {
        this.da = da;
    }

    public static HuangBtn getBaozi() {
        return baozi;
    }

    public void setBaozi(HuangBtn baozi) {
        this.baozi = baozi;
    }

    public static JTextField getGold() {
        return gold;
    }

    public void setGold(JTextField gold) {
        this.gold = gold;
    }

    public long getGold0() {
        return gold0;
    }

    public void setGold0(long gold0) {
        this.gold0 = gold0;
    }

    public static HuangBtn getXuanyu() {
        return xuanyu;
    }

    public void setXuanyu(HuangBtn xuanyu) {
        this.xuanyu = xuanyu;
    }

    public static HuangBtn getJinbi() {
        return jinbi;
    }

    public void setJinbi(HuangBtn jinbi) {
        this.jinbi = jinbi;
    }

    public static HuangBtn getXiao() {
        return xiao;
    }

    public void setXiao(HuangBtn xiao) {
        this.xiao = xiao;
    }

    public static HuangBtn getMai() {
        return mai;
    }

    public void setMai(HuangBtn mai) {
        this.mai = mai;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getTpye1() {
        return tpye1;
    }

    public void setTpye1(int tpye1) {
        this.tpye1 = tpye1;
    }
}
